const { Router } = require("express");
const { body, validationResult } = require("express-validator");
const hospitalCtrl = require("../controllers/hospitalCtrl");
const auth = require("../middleware/auth");

const router = new Router();

// 验证中间件
const validateRequest = (req, res, next) => {
    const result = validationResult(req);
    if (result.isEmpty()) {
        next();
    } else {
        const errors = result.array().map(err => err.msg).join(', ');
        res.json({ 
            code: 1,
            message: errors 
        });
    }
};

// ===== 科室管理 =====

/**
 * 获取科室列表 /api/hospital/departments
 * GET
 */
router.get('/departments', auth, hospitalCtrl.getDepartments);

/**
 * 创建科室 /api/hospital/departments
 * POST
 */
router.post('/departments', [
    auth,
    body('name').notEmpty().withMessage('科室名称不能为空'),
    body('code').notEmpty().withMessage('科室代码不能为空'),
    validateRequest
], hospitalCtrl.createDepartment);

/**
 * 更新科室 /api/hospital/departments/:id
 * PUT
 */
router.put('/departments/:id', auth, hospitalCtrl.updateDepartment);

/**
 * 删除科室 /api/hospital/departments/:id
 * DELETE
 */
router.delete('/departments/:id', auth, hospitalCtrl.deleteDepartment);

/**
 * 批量删除科室 /api/hospital/departments/batch
 * DELETE
 */
router.delete('/departments/batch', auth, hospitalCtrl.batchDeleteDepartments);

// ===== 通知管理 =====

/**
 * 获取通知列表 /api/hospital/notices
 * GET
 */
router.get('/notices', hospitalCtrl.getNotices);

/**
 * 获取通知详情 /api/hospital/notices/:id
 * GET
 */
router.get('/notices/:id', hospitalCtrl.getNoticeDetail);

/**
 * 创建通知 /api/hospital/notices
 * POST
 */
router.post('/notices', [
    auth,
    body('title').notEmpty().withMessage('通知标题不能为空'),
    body('content').notEmpty().withMessage('通知内容不能为空'),
    validateRequest
], hospitalCtrl.createNotice);

/**
 * 更新通知 /api/hospital/notices/:id
 * PUT
 */
router.put('/notices/:id', auth, hospitalCtrl.updateNotice);

/**
 * 删除通知 /api/hospital/notices/:id
 * DELETE
 */
router.delete('/notices/:id', auth, hospitalCtrl.deleteNotice);

/**
 * 批量删除通知 /api/hospital/notices/batch
 * DELETE
 */
router.delete('/notices/batch', auth, hospitalCtrl.batchDeleteNotices);

// ===== 医院信息管理 =====

/**
 * 获取医院信息列表 /api/hospital/infos
 * GET
 */
router.get('/infos', hospitalCtrl.getHospitalInfos);

/**
 * 获取医院信息详情 /api/hospital/infos/:id
 * GET
 */
router.get('/infos/:id', hospitalCtrl.getHospitalInfoDetail);

/**
 * 创建医院信息 /api/hospital/infos
 * POST
 */
router.post('/infos', [
    auth,
    body('title').notEmpty().withMessage('标题不能为空'),
    body('content').notEmpty().withMessage('内容不能为空'),
    body('type').notEmpty().withMessage('信息类型不能为空'),
    validateRequest
], hospitalCtrl.createHospitalInfo);

/**
 * 更新医院信息 /api/hospital/infos/:id
 * PUT
 */
router.put('/infos/:id', auth, hospitalCtrl.updateHospitalInfo);

/**
 * 删除医院信息 /api/hospital/infos/:id
 * DELETE
 */
router.delete('/infos/:id', auth, hospitalCtrl.deleteHospitalInfo);

// ===== 反馈管理 =====

/**
 * 获取反馈列表 /api/hospital/feedbacks
 * GET
 */
router.get('/feedbacks', auth, hospitalCtrl.getFeedbacks);

/**
 * 获取反馈详情 /api/hospital/feedbacks/:id
 * GET
 */
router.get('/feedbacks/:id', auth, hospitalCtrl.getFeedbackDetail);

/**
 * 创建反馈 /api/hospital/feedbacks
 * POST
 */
router.post('/feedbacks', [
    body('title').notEmpty().withMessage('反馈标题不能为空'),
    body('content').notEmpty().withMessage('反馈内容不能为空'),
    body('userName').notEmpty().withMessage('姓名不能为空'),
    validateRequest
], hospitalCtrl.createFeedback);

/**
 * 更新反馈 /api/hospital/feedbacks/:id
 * PUT
 */
router.put('/feedbacks/:id', auth, hospitalCtrl.updateFeedback);

/**
 * 删除反馈 /api/hospital/feedbacks/:id
 * DELETE
 */
router.delete('/feedbacks/:id', auth, hospitalCtrl.deleteFeedback);

module.exports = router; 